package cn.liweixi.basic.service.impl;

import cn.liweixi.basic.mapper.BaseMapper;
import cn.liweixi.basic.query.BaseQuery;
import cn.liweixi.basic.service.IBaseService;
import cn.liweixi.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseSeviceImpl<T> implements IBaseService<T> {
    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    public List findAll() {
        return baseMapper.findAll();
    }

    @Override
    public T findById(Long id) {
        return baseMapper.findById(id);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        baseMapper.delete(id);
    }

    @Override
    @Transactional
    public void add(T t) {
        baseMapper.add(t);
    }

    @Override
    @Transactional
    public void updateById(T t) {
        baseMapper.updateById(t);
    }

    @Override
    public PageList<T> queryByPage(BaseQuery baseQuery) {
        //查询数量
        Integer totals = baseMapper.queryByCount(baseQuery);

        if(totals < 1){
            return new PageList<T>();
        }

        //查询数据
        List<T> rows = baseMapper.queryByPage(baseQuery);
        return new PageList<T>(totals,rows);
    }

    @Override
    public void batchDel(List ids) {
        baseMapper.batchDel(ids);
    }
}
